package com.scalar.chiptrack.workflow;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.jgraph.JGraph;
import com.jgraph.graph.ConnectionSet;
import com.jgraph.graph.DefaultEdge;
import com.jgraph.graph.DefaultGraphCell;
import com.jgraph.graph.DefaultPort;
import com.jgraph.graph.GraphConstants;
import com.jgraph.graph.PortView;

public class WorkflowManager implements Serializable, Cloneable {
	
	private final static String XMLNS = "https://www.wfmc.org/2002/XPDL1.0";
   	private final static String XMLNS_XPDL = "https://www.wfmc.org/2002/XPDL1.0";
   	private final static String XMLNS_XSI = "https://www.w3.org/2001/XMLSchema-instance";
   	private final static String XSI_SCHEMA_LOCATION = "https://www.wfmc.org/2002/XPDL1.0 http://wfmc.org/standards/docs/TC-1025_schema_10_xpdl.xsd";
   
	protected WorkflowGraph graph;
	
	public WorkflowManager(JGraph graph){
		this.graph = (WorkflowGraph)graph;
	}
	
	public WorkflowManager( ){}
	
	public WorkflowModel getModel(){
		return (WorkflowModel)graph.getModel();
	}
	
	public ArrayList getActivities(){
		return getModel().getAllCells();
	}
	
	public ArrayList getAllTransitions(){
		return getModel().getAllEdges();
	}
	
	public DefaultGraphCell getActivity(String id){
		return getModel().getCell(id);
	}
	
	public DefaultPort getPort(Object cell){
		return getModel().getPort(cell);
	}
	
	public void toXML(Node parent, ArrayList cellsList, ArrayList edgesList, String location )
    {

        String editorName = null;
		Node node = ((Document)parent).createElement("Package");
        if ( "ClipBoard".equalsIgnoreCase( location ) )
        {
            editorName = "clipboard";
        }
        else
        {
            editorName = graph.getEditor().getName();
        }
		((Element)node).setAttribute("Id", editorName);
		((Element)node).setAttribute("xmlns", XMLNS);
		((Element)node).setAttribute("xmlns:xpdl", XMLNS_XPDL);
		((Element)node).setAttribute("xmlns:xsi", XMLNS_XSI);
		((Element)node).setAttribute("xsi:schemaLocation", XSI_SCHEMA_LOCATION);
		
		Node workflowProcesses = (node.getOwnerDocument()).createElement("Workflowprocesses");
		
		Node workflowProcess = (workflowProcesses.getOwnerDocument()).createElement("workflowprocess");
		((Element)workflowProcess).setAttribute("Id", editorName+"_Work");

		cellToXML(workflowProcess, editorName , cellsList );
		edgeToXML(workflowProcess, editorName, edgesList );
		workflowProcesses.appendChild(workflowProcess);
		node.appendChild(workflowProcesses);
		parent.appendChild(node);
	}
	
	public void cellToXML(Node parent, String name, ArrayList cellsList)
	{
        ArrayList list = new ArrayList();
        boolean isClipBoard = false;
		try
		{
		Node activities 	= ( parent.getOwnerDocument() ).createElement( "Activities" );
        if ( "clipboard".equalsIgnoreCase(name ) )
        {
              if ( cellsList != null && cellsList.size() > 0 )
              list.addAll( cellsList );
              isClipBoard = true;
		}
        else
        {
             list 		= getActivities();
        }

		for( int i = 0; i < list.size(); i++ )
		{
			DefaultGraphCell cell 	= ( DefaultGraphCell )list.get( i );
			Map map 				= cell.getAttributes();
			Node activity 			= (activities.getOwnerDocument()).createElement("Activity");
			String attrId 			=  name+"_Act_"+(i+1);
			String processStepId 	= null;
			String tempId 			= null;
			String type 			= "";
            String processName      = "";
            int firstStrongIndex    = -1;
            int lastStrongIndex     = -1;

			if( map.containsKey( "processStepId" ) && !isClipBoard )
				processStepId = (String)map.get( "processStepId" );
			
			if( processStepId == null )
			{
				map.put( "processStepId", "" );
				cell.changeAttributes( map );
			}
			
			map.put("attrId", attrId);
			cell.changeAttributes(map);

			((Element)activity).setAttribute("Id", attrId);
			
			Object tempValue = "";
			if( map.containsKey( "value" ) )
				tempValue = map.get("value");

            if ( tempValue != null && ((String)tempValue).length() > 0 && ((String)tempValue).indexOf("<STRONG>") > 0 && ((String)tempValue).indexOf("</STRONG>") > 0 )
            {
                firstStrongIndex = ((String)tempValue).indexOf("<STRONG>") + "<STRONG>".length();
                lastStrongIndex  = ((String)tempValue).indexOf("</STRONG>");
                if ( firstStrongIndex != -1 && lastStrongIndex != -1 )
                {
                    processName = ((String)tempValue).substring(firstStrongIndex, lastStrongIndex);
                }
            }
            else
            {
                processName = (String)tempValue;
            }

			if(tempValue instanceof String)
			{
				//((Element)activity).setAttribute( "name", (String)tempValue );
                ((Element)activity).setAttribute( "name", processName );
			}

			if( map.containsKey("Type") )
				type = (String)map.get("Type");

			((Element)activity).setAttribute("Type", type);
			
			Node extendedAttributes = (activity.getOwnerDocument()).createElement("ExtendedAttributes");
			Node extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 

			if( map.containsKey("bounds") )
			{                              
				Rectangle r = (Rectangle)map.get("bounds");
				((Element)extendedAttribute).setAttribute("Name", "bounds");
                if ( isClipBoard )
				((Element)extendedAttribute).setAttribute("Value", ("X:"+((r.x))+";"+"Y:"+((r.y)+30)+";"+"W:"+r.width+";"+"H:"+r.height));
                else
                ((Element)extendedAttribute).setAttribute("Value", ("X:"+r.x+";"+"Y:"+r.y+";"+"W:"+r.width+";"+"H:"+r.height));
			}
			else
			{
				((Element)extendedAttribute).setAttribute("Name", "bounds");
				((Element)extendedAttribute).setAttribute("Value", ("X:"+10+";"+"Y:"+10+";"+"W:"+70+";"+"H:"+30));
			}
			
			extendedAttributes.appendChild(extendedAttribute);
			
			extendedAttribute = (extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			
			((Element)extendedAttribute).setAttribute("Name", "processStepId");
			((Element)extendedAttribute).setAttribute("Value", (String)map.get( "processStepId" ) );
			
			extendedAttributes.appendChild(extendedAttribute);
			
			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			
			((Element)extendedAttribute).setAttribute("Name", "partnumber");
			
			if( map.containsKey( "partnumber") )
				((Element)extendedAttribute).setAttribute("Value", "clipboard".equalsIgnoreCase(name )? "clipboard" : (String)map.get("partnumber"));
			else	
				((Element)extendedAttribute).setAttribute("Value", "");
			
			extendedAttributes.appendChild(extendedAttribute);

            extendedAttribute = (extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
            ((Element)extendedAttribute).setAttribute("Name", "partStatus");
            ((Element)extendedAttribute).setAttribute("Value", map.get( "partStatus" ) != null ? (String)map.get( "partStatus" ) : "Prod" );

            extendedAttributes.appendChild(extendedAttribute);

            if ( processStepId == null || processStepId.trim().length() == 0 ) //un saved process details are saving to XML as the process details are loading from DB when clicked
            {
                extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
			    ((Element)extendedAttribute).setAttribute("Name", "parameters");
                if( map.containsKey( "parameters") )
                    ((Element)extendedAttribute).setAttribute("Value", (String)map.get("parameters"));
                else
                    ((Element)extendedAttribute).setAttribute("Value", "");
                extendedAttributes.appendChild(extendedAttribute);

			    extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
                ((Element)extendedAttribute).setAttribute("Name", "dupPartNumbers");
                if( map.containsKey("dupPartNumbers") )
                    ((Element)extendedAttribute).setAttribute("Value", (String)map.get("dupPartNumbers") );
                else
                    ((Element)extendedAttribute).setAttribute("Value", "");
                extendedAttributes.appendChild(extendedAttribute);
			
			    extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
                ((Element)extendedAttribute).setAttribute("Name", "comments");
                if( map.containsKey("comments") )
                    ((Element)extendedAttribute).setAttribute("Value", (String)map.get("comments"));
                else
                    ((Element)extendedAttribute).setAttribute("Value", "");
                extendedAttributes.appendChild(extendedAttribute);
                /*
                extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
                ((Element)extendedAttribute).setAttribute("Name", "vendors");
                if( map.containsKey("vendors") )
                    ((Element)extendedAttribute).setAttribute("Value", (String)map.get("vendors"));
                else
                    ((Element)extendedAttribute).setAttribute("Value", "");
                extendedAttributes.appendChild(extendedAttribute);
                */
                extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
                ((Element)extendedAttribute).setAttribute("Name", "vendors");
                if( map.containsKey("vendors") )
                {
                    String vendorsString = (String)map.get("vendors");
                    ArrayList vendorsList 	= graph.getWorkflowManager().parseString( vendorsString , '#' );

                    Iterator it 		= vendorsList.iterator();
                    while( it.hasNext() )
                    {
                        Node vendor =(extendedAttribute.getOwnerDocument()).createElement("Vendor");

                        String  vendorDetailsString = (String)it.next();
                        if ( vendorDetailsString != null && vendorDetailsString.trim().length() > 0 )
                        {
                            //ArrayList vendorDetails  = graph.getWorkflowManager().parseString((String)it.next(),';');
                            ArrayList vendorDetails  = graph.getWorkflowManager().parseString(vendorDetailsString,';');


                            for ( int vendAttr = 0; vendAttr < vendorDetails.size() && vendorDetails.size() != 0 ; vendAttr++ )
                            {
                                Node vendorAttribute =(extendedAttribute.getOwnerDocument()).createElement("VendorAttribute");
                                switch ( vendAttr )
                                {
                                    case 0:
                                        ((Element)vendorAttribute).setAttribute("Name", "select");
                                        ((Element)vendorAttribute).setAttribute("Value", (String)vendorDetails.get(vendAttr));
                                        break;
                                    case 1:
                                        ((Element)vendorAttribute).setAttribute("Name", "priority");
                                        ((Element)vendorAttribute).setAttribute("Value", (String)vendorDetails.get(vendAttr));
                                        break;
                                    case 2:
                                        ((Element)vendorAttribute).setAttribute("Name", "locationName");
                                        ((Element)vendorAttribute).setAttribute("Value", (String)vendorDetails.get(vendAttr));
                                        break;
                                    case 3:
                                        ((Element)vendorAttribute).setAttribute("Name", "split");
                                        ((Element)vendorAttribute).setAttribute("Value", (String)vendorDetails.get(vendAttr));
                                        break;
                                    case 4:
                                        ((Element)vendorAttribute).setAttribute("Name", "capacity/weeek");
                                        ((Element)vendorAttribute).setAttribute("Value", (String)vendorDetails.get(vendAttr));
                                        break;
                                    case 5:
                                        ((Element)vendorAttribute).setAttribute("Name", "cycleTimeNormal");
                                        ((Element)vendorAttribute).setAttribute("Value", (String)vendorDetails.get(vendAttr));
                                        break;
                                    case 6:
                                        ((Element)vendorAttribute).setAttribute("Name", "cycleTimeExpress");
                                        ((Element)vendorAttribute).setAttribute("Value", (String)vendorDetails.get(vendAttr));
                                        break;
                                }
                                vendor.appendChild(vendorAttribute);
                            }
                            extendedAttribute.appendChild(vendor);
                        }
                    }
                    extendedAttributes.appendChild(extendedAttribute);
                }

				if( map.containsKey("mktPartNumber") )
			    {
                    extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
                    ((Element)extendedAttribute).setAttribute("Name", "mktPartNumber");
                    ((Element)extendedAttribute).setAttribute("Value", (String)map.get("mktPartNumber"));
                    extendedAttributes.appendChild(extendedAttribute);
			    }
			    if( map.containsKey("isApprovedProduct") )
			    {
				    extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
                    ((Element)extendedAttribute).setAttribute("Name", "isApprovedProduct");
                    ((Element)extendedAttribute).setAttribute("Value", (String)map.get("isApprovedProduct"));
                    extendedAttributes.appendChild(extendedAttribute);
                }
                if( map.containsKey("isPrimaryFGProduct") )
			    {
				    extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
                    ((Element)extendedAttribute).setAttribute("Name", "isPrimaryFGProduct");
                    ((Element)extendedAttribute).setAttribute("Value", (String)map.get("isPrimaryFGProduct"));
                    extendedAttributes.appendChild(extendedAttribute);
                }
                if( map.containsKey("binsRequired") )
                {
                    extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
                    ((Element)extendedAttribute).setAttribute("Name", "binsRequired");
                    ((Element)extendedAttribute).setAttribute("Value", (String)map.get("binsRequired"));
                    extendedAttributes.appendChild(extendedAttribute);
                }
                if( map.containsKey("goodBinNumber") )
                {
                    extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
                    ((Element)extendedAttribute).setAttribute("Name", "goodBinNumber");
                    ((Element)extendedAttribute).setAttribute("Value", (String)map.get("goodBinNumber"));
                    extendedAttributes.appendChild(extendedAttribute);
                }
                if( map.containsKey("rejectedBinNumber") )
                {
                    extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
                    ((Element)extendedAttribute).setAttribute("Name", "rejectedBinNumber");
                    ((Element)extendedAttribute).setAttribute("Value", (String)map.get("rejectedBinNumber"));
                    extendedAttributes.appendChild(extendedAttribute);
                }
                extendedAttribute = (extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
                ((Element)extendedAttribute).setAttribute("Name", "productGroup");
                ((Element)extendedAttribute).setAttribute("Value", map.get( "productGroup" ) != null ? (String)map.get( "productGroup" ) : "" );
                extendedAttributes.appendChild(extendedAttribute);
            }
            
            activity.appendChild(extendedAttributes);
			activities.appendChild(activity);
		}
		parent.appendChild(activities);
		}
		catch(Exception e)
		{
			System.out.println("I am carrying the error");
			e.printStackTrace();
		}
	}
	
	public void edgeToXML( Node parent, String name, ArrayList edgesList )
	{
        ArrayList list = new ArrayList();
        if ( "clipboard".equalsIgnoreCase(name ))
        {
            if (edgesList != null && edgesList.size() > 0 )
                list.addAll( edgesList );
		}
        else
        {
             list 		= getAllTransitions();
        }
		//ArrayList list 		= getAllTransitions();
		Node transitions 	= (parent.getOwnerDocument()).createElement("Transitions");
		
		for( int i = 0; i < list.size(); i++ )
		{
			DefaultEdge edge = (DefaultEdge)list.get( i );
            if ( edge != null )
            {
                Map map 		 = edge.getAttributes();
                DefaultGraphCell sourceCell = (DefaultGraphCell)((DefaultPort)edge.getSource()).getParent();
                DefaultGraphCell targetCell = (DefaultGraphCell)((DefaultPort)edge.getTarget()).getParent();

                String from 	= (String)( sourceCell.getAttributes().get( "attrId" ) );
                String to 		= (String)( targetCell.getAttributes().get( "attrId" ) );

                String edgeId = name+"_Tran_"+(i+1);

                if( map.get( "edgeId" ) == null )
                {
                    map.put( "edgeId", edgeId );
                    edge.changeAttributes( map );
                }
			
                String transitionStepId = (String)map.get("transitionStepId");

                if( transitionStepId == null )
                {
                    map.put( "transitionStepId", "" );
                    edge.changeAttributes( map );
                }
			
                String value = (String)map.get( "value" );
                if( value == null )
                {
                    value = "Transition";
                }
			
                Node transition = ( transitions.getOwnerDocument() ).createElement( "Transition" );
                ( (Element)transition ).setAttribute( "Id", edgeId );
                ( (Element)transition ).setAttribute( "From", from );
                ( (Element)transition ).setAttribute( "To", to );
                ( (Element)transition ).setAttribute( "Name", value );
			
                Node extendedAttributes = ( transition.getOwnerDocument() ).createElement( "ExtendedAttributes" );
                Node extendedAttribute 	=( extendedAttributes.getOwnerDocument() ).createElement( "ExtendedAttribute" );

                Rectangle r = ( Rectangle )map.get( "bounds" );
                ( (Element)extendedAttribute ).setAttribute( "Name", "bounds" );
                ( (Element)extendedAttribute ).setAttribute( "Value", ("X:"+r.x+";"+"Y:"+r.y+";"+"W:"+r.width+";"+"H:"+r.height ) );
			
                extendedAttributes.appendChild( extendedAttribute );

                extendedAttribute = ( extendedAttributes.getOwnerDocument() ).createElement( "ExtendedAttribute" );
                ( (Element)extendedAttribute ).setAttribute( "Name", "transitionStepId" );
                ( (Element)extendedAttribute ).setAttribute( "Value", (String)map.get("transitionStepId") );
			
                extendedAttributes.appendChild( extendedAttribute );

                extendedAttribute = ( extendedAttributes.getOwnerDocument() ).createElement( "ExtendedAttribute" );
                ( (Element)extendedAttribute ).setAttribute( "Name", "transportLocations" );

                if( map.containsKey( "transportLocations" ) )
                {
                    ( (Element)extendedAttribute ).setAttribute( "Value", (String)map.get("transportLocations") );
                }
                else
                    ( (Element)extendedAttribute ).setAttribute( "Value", "" );

                extendedAttributes.appendChild( extendedAttribute );

                /*String lineEnd = ((Integer)map.get("lineEnd")).toString();

                extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");

                ((Element)extendedAttribute).setAttribute("Name", "lineEnd");
                ((Element)extendedAttribute).setAttribute("Value", lineEnd);

                extendedAttributes.appendChild(extendedAttribute);

                //ArrayList pointsList = (ArrayList)map.get("points");

                Rectangle rect = (Rectangle)((Map)sourceCell.getAttributes()).get("bounds");
                //System.out.println("Source Rect = "+rect);
                Point p = new Point((rect.x+rect.width)/2, (rect.y+rect.height)/2);
                //System.out.println("Source Point = "+p);

                extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");

                ((Element)extendedAttribute).setAttribute("Name", "source");
                ((Element)extendedAttribute).setAttribute("Value", ("X:"+p.x+";Y:"+p.y));

                extendedAttributes.appendChild(extendedAttribute);

                rect = (Rectangle)((Map)targetCell.getAttributes()).get("bounds");
                //System.out.println("Target Rect = "+rect);
                p = new Point((rect.x+rect.width)/2, (rect.y+rect.height)/2);
                //System.out.println("Target Point = "+p);

                extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");

                ((Element)extendedAttribute).setAttribute("Name", "target");
                ((Element)extendedAttribute).setAttribute("Value", ("X:"+p.x+";Y:"+p.y));

                extendedAttributes.appendChild(extendedAttribute);
                */

                transition.appendChild( extendedAttributes );
                transitions.appendChild( transition );
            }   
		}
		
		parent.appendChild( transitions );
	}
	
	
	public void createWorkflow(String uri, String type)
	{
		ArrayList activityCollection = new ArrayList();
		ArrayList transitionCollection = new ArrayList();
		try{
			Document document = null;
			DocumentBuilderFactory dbf 	= DocumentBuilderFactory.newInstance();
			DocumentBuilder builder 	= dbf.newDocumentBuilder();
			document = builder.parse( uri );
            buildActivitiesAndTransitionCollection(document, activityCollection, transitionCollection, type);
		}catch(Exception e){
			if( !(e instanceof FileNotFoundException) )
			e.printStackTrace();
			return;
		}
	
		if(activityCollection.isEmpty()){
			System.out.println("This is empty List");
		}

	    Object newCells[] = new Object[activityCollection.size()];
        int cells = 0;
		Iterator it = activityCollection.iterator();
		while(it.hasNext()){
			insertActivities((Map)it.next() , newCells, type, cells++);
		}

        Object newEdge[] = new Object[transitionCollection.size()];
        int edges = 0;
		it = transitionCollection.iterator();
		while(it.hasNext()){
			insertTransitions((Map)it.next(), newEdge, type, edges++ );
		}
	}
	
	public void createWorkflow(File file){
		
		ArrayList activityCollection = new ArrayList();
		ArrayList transitionCollection = new ArrayList();
		
		try{
			
			Document document = null;
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = dbf.newDocumentBuilder();
			
			document = builder.parse(file);
		
			buildActivitiesAndTransitionCollection(document, activityCollection, transitionCollection, null);
			
		}catch(Exception e){
			e.printStackTrace();
			return;
		}
		
		if(activityCollection.isEmpty()){
			System.out.println("This is empty List");
		}
        Object newCells[] = new Object[activityCollection.size() ];
        int cells = 0;
		Iterator it = activityCollection.iterator();
		while(it.hasNext()){
			insertActivities((Map)it.next(), newCells, null , cells++);
		}
		Object newEdge[] = new Object[transitionCollection.size()];
        int edges = 0;
		it = transitionCollection.iterator();
		while(it.hasNext()){
			insertTransitions((Map)it.next(), newEdge, null, edges++);
		}
	}
	
	
	public void createWorkflowFromDoc(Document doc){
		
		ArrayList activityCollection = new ArrayList();
		ArrayList transitionCollection = new ArrayList();
	
		try
		{
			buildActivitiesAndTransitionCollection( doc, activityCollection, transitionCollection,null );
		}
		catch(Exception e){
			e.printStackTrace();
			return;
		}
	
		if(activityCollection.isEmpty()){
			System.out.println("This is empty List");
		}
	    Object newCells[] = new Object[activityCollection.size()];
        int cells = 0;
		Iterator it = activityCollection.iterator();
		while(it.hasNext()){
			insertActivities((Map)it.next(), newCells, null, cells++);
		}
	    Object newEdge[] = new Object[transitionCollection.size()];
        int edges = 0;
		it = transitionCollection.iterator();
		while(it.hasNext()){
			insertTransitions((Map)it.next(), newEdge, null, edges++);
		}
	}
	
	public void buildActivitiesAndTransitionCollection(Document parent, ArrayList activities, ArrayList transitionList, String type )
    {
        String timestamp = System.currentTimeMillis()+"";
        NodeList nodeList = parent.getElementsByTagName("Activities");
				
		for(int i =0; i<nodeList.getLength();i++){
			Element element = (Element)nodeList.item(i);
			NodeList activity = element.getElementsByTagName("Activity");
			
			createActivityMaps(activity, activities, type, timestamp);
		}
		
		nodeList = parent.getElementsByTagName("Transitions");
		
		for(int i =0; i<nodeList.getLength();i++){
			Element element = (Element)nodeList.item(i);
			NodeList transition = element.getElementsByTagName("Transition");
			createTransitionMaps(transition, transitionList, type, timestamp);
		}
	}
	
	public void createActivityMaps(NodeList activity, ArrayList list, String type, String timeStamp )
	{
		Map map = null;
		String partNumber  = "";
		String processName = "";
        String vendors = "";
        String partStatus = "";
		for(int i=0; i<activity.getLength(); i++)
		{
			Element element = (Element)activity.item(i);
			map = new HashMap();

			//map.put( "name", element.getAttribute( "name" )  );
			map.put( "value", element.getAttribute( "name" ) );
			processName = element.getAttribute( "name" ) ;
            if ( "clipboard".equalsIgnoreCase(type) )
            {
			    map.put( "attrId", element.getAttribute( "Id" ) +  timeStamp );
            }
            else
            {
                map.put( "attrId", element.getAttribute( "Id" ) );
            }
			map.put( "Type", element.getAttribute( "Type" ) );
            //System.out.println( "Base Product Name  " + (String)map.get( "value" ) + "   process type " + (String)map.get( "Type" ) );
			NodeList extendedAttribute = element.getElementsByTagName( "ExtendedAttribute" );
			
			for(int extAttr =0; extAttr<extendedAttribute.getLength(); extAttr++)
            {
				element = (Element)extendedAttribute.item(extAttr);
				
				if(element.getAttribute("Name").equalsIgnoreCase("bounds"))
                {
					String value = element.getAttribute("Value");
                    if ("clipboard".equalsIgnoreCase(type) )
                    {
                        //X:462;Y:60;W:70;H:30
                        String beforeY = value.split("Y:")[0];
                        String afterY = value.split("Y:")[1];

                        int yvalue = Integer.parseInt( afterY.substring(0, afterY.indexOf(";") ) ) + 20;

                        value =  beforeY + "Y:" + yvalue + afterY.substring(afterY.indexOf(";"));
                                                
				        map.put("bounds", getRectangle(value) );
				    }
                    else
                    {
					    map.put("bounds", getRectangle(value) );
                    }
				}
				if(element.getAttribute("Name").equalsIgnoreCase("processStepId")){
					map.put("processStepId", element.getAttribute("Value"));
				}
				if(element.getAttribute("Name").equalsIgnoreCase("partnumber")){
					map.put("partnumber", element.getAttribute("Value"));
					partNumber = element.getAttribute("Value");
				}
				if(element.getAttribute("Name").equalsIgnoreCase("parameters")){
					map.put("parameters", element.getAttribute("Value"));
				}
				if(element.getAttribute("Name").equalsIgnoreCase("dupPartNumbers"))
				{
					map.put("dupPartNumbers", element.getAttribute("Value"));
				}
				if(element.getAttribute("Name").equalsIgnoreCase("mktPartNumber"))
				{
					map.put("mktPartNumber", element.getAttribute("Value"));
				}
				if(element.getAttribute("Name").equalsIgnoreCase("comments"))
				{
					map.put("comments", element.getAttribute("Value"));
				}
                /*
				if(element.getAttribute("Name").equalsIgnoreCase("vendors"))
				{
					map.put("vendors", element.getAttribute("Value"));
				}
                */

                if(element.getAttribute("Name").equalsIgnoreCase("vendors"))
                {
                    //map.put("vendors", element.getAttribute("Value"));
                    vendors = element.getAttribute("Value");

                    StringBuffer vendorString = new StringBuffer();
                    if ( vendors != null && vendors.trim().length() != 0 )
                    {
                        ArrayList vendorsList = parseString(vendors,'#');

                        if( vendorsList != null && vendorsList.size() > 0 )
                        {
                            for ( int vendorCount = 0 ; vendorCount < vendorsList.size() - 1 ; vendorCount++ )
                            {
                                ArrayList vendorInfo = parseString( (String)vendorsList.get(vendorCount) , ';');
                                vendorString.append("0");       //Represents VendorId
                                vendorString.append((";"));
                                vendorString.append("0");       //Represents LocationId

                                for ( int attrCount = 0 ; attrCount < vendorInfo.size(); attrCount++ )
                                {
                                    //UnitCost/Normal, UnitCost/Express AND Expected Yeild Not Required.
                                    if ( attrCount != 5 && attrCount != 6 && attrCount != 9 )
                                    {
                                        vendorString.append((";"));
                                        vendorString.append( vendorInfo.get(attrCount));
                                    }
                                }
                                vendorString.append("#");
                            }
                        }
                        map.put("vendors", vendorString.toString() );
                    }
                    else
                    {
                        NodeList vendorsNodeList = element.getElementsByTagName( "Vendor" );
                        Element vendorElement;
                        NodeList vendorAttributes;
                        Element vendorAttribute;

                        for(int vendTagCount =0; vendTagCount < vendorsNodeList.getLength(); vendTagCount++)
                        {
                                vendorElement = (Element)vendorsNodeList.item(vendTagCount);
                                vendorAttributes = vendorElement.getElementsByTagName("VendorAttribute");

                            for(int vendorAttr =0; vendorAttr < vendorAttributes.getLength(); vendorAttr++)
                            {
                                vendorAttribute = (Element)vendorAttributes.item(vendorAttr);

                                if ( vendorAttribute.getAttribute("Name").equalsIgnoreCase("vendorId") )
                                {
                                    vendorString.append( vendorAttribute.getAttribute("Value") );
                                    vendorString.append(";");
                                }

                                if ( vendorAttribute.getAttribute("Name").equalsIgnoreCase("locationId") )
                                {
                                    vendorString.append( vendorAttribute.getAttribute("Value") );
                                    vendorString.append(";");
                                }

                                if ( vendorAttribute.getAttribute("Name").equalsIgnoreCase("select") )
                                {
                                    vendorString.append( vendorAttribute.getAttribute("Value") );
                                    vendorString.append(";");
                                }
                                if ( vendorAttribute.getAttribute("Name").equalsIgnoreCase("priority") )
                                {
                                    vendorString.append( vendorAttribute.getAttribute("Value") );
                                    vendorString.append(";");
                                }
                                if ( vendorAttribute.getAttribute("Name").equalsIgnoreCase("locationName") )
                                {
                                    vendorString.append( vendorAttribute.getAttribute("Value") );
                                    vendorString.append(";");
                                }
                                if ( vendorAttribute.getAttribute("Name").equalsIgnoreCase("split") )
                                {
                                    vendorString.append( vendorAttribute.getAttribute("Value") );
                                    vendorString.append(";");
                                }
                                if ( vendorAttribute.getAttribute("Name").equalsIgnoreCase("capacity/weeek") )
                                {
                                    vendorString.append( vendorAttribute.getAttribute("Value") );
                                    vendorString.append(";");
                                }
                                if ( vendorAttribute.getAttribute("Name").equalsIgnoreCase("cycleTimeNormal") )
                                {
                                    vendorString.append( vendorAttribute.getAttribute("Value") );
                                    vendorString.append(";");
                                }
                                if ( vendorAttribute.getAttribute("Name").equalsIgnoreCase("cycleTimeExpress") )
                                {
                                    vendorString.append( vendorAttribute.getAttribute("Value") );
                                }
                            }
                            vendorString.append("#");
                        }
                        map.put("vendors", vendorString.toString());
                    }
                }

				if(element.getAttribute("Name").equalsIgnoreCase("isApprovedProduct"))
				{
					map.put("isApprovedProduct", element.getAttribute("Value"));
				}
                if(element.getAttribute("Name").equalsIgnoreCase("isPrimaryFGProduct"))
				{
					map.put("isPrimaryFGProduct", element.getAttribute("Value"));
				}
                if(element.getAttribute("Name").equalsIgnoreCase("goodBinNumber"))
				{
					map.put("goodBinNumber", element.getAttribute("Value"));
				}
				if(element.getAttribute("Name").equalsIgnoreCase("rejectedBinNumber"))
				{
					map.put("rejectedBinNumber", element.getAttribute("Value"));
				}
				if(element.getAttribute("Name").equalsIgnoreCase("binsRequired"))
				{
					map.put("binsRequired", element.getAttribute("Value"));
				}
                if(element.getAttribute("Name").equalsIgnoreCase("partStatus"))
				{
					map.put("partStatus", element.getAttribute("Value"));
                    partStatus = element.getAttribute("Value");
                }
                if(element.getAttribute("Name").equalsIgnoreCase("productGroup"))
				{
					map.put("productGroup", element.getAttribute("Value"));
				}
            }

			//map.put( "value", processName + System.getProperty( "line.separator" ) + partNumber );
            //map.put( "value", processName + "/" + partNumber );
            StringBuffer sb = new StringBuffer(50);
            sb.append("<html>");
            sb.append("<body align='center'><font face='Arial' size='2'>");
            sb.append("<STRONG>" + processName + "</STRONG>"+ "<BR>" + "<STRONG>" + partNumber + "</STRONG>" );
            sb.append("</body>");
            sb.append("</html>");
            map.put( "value", sb.toString());
            if ( "Obsolete".equalsIgnoreCase( partStatus ) )
            {
                //set the back ground color to red because processStepId is obsolete
                GraphConstants.setBackground(map, new Color(255, 84, 74));
            }
            else if ( "Last Buy".equalsIgnoreCase( partStatus ) )
            {
                GraphConstants.setBackground(map, new Color(255, 205, 202));
            }
			list.add(map);
		}
	}

	public void createTransitionMaps(NodeList transition, ArrayList list,String type, String timeStamp){

		Map map = null;

		for(int i=0; i<transition.getLength(); i++){

			Element element = (Element)transition.item(i);
			//System.out.println("createTransitionMaps Name = "+element.getAttribute("Name"));

			map = new HashMap();
			map.put("value", element.getAttribute("name"));
			//map.put("opaque", true);
            if ("clipboard".equalsIgnoreCase( type ) )
            {
                map.put("From", element.getAttribute("From") + timeStamp );
			    map.put("To", element.getAttribute("To") + timeStamp );
            }
            else
            {
			    map.put("From", element.getAttribute("From")  );
			    map.put("To", element.getAttribute("To"));
            }

			NodeList extendedAttribute = element.getElementsByTagName("ExtendedAttribute");

			for(int extAttr =0; extAttr<extendedAttribute.getLength(); extAttr++){
				element = (Element)extendedAttribute.item(extAttr);

				if(element.getAttribute("Name").equalsIgnoreCase("bounds")){
					String value = element.getAttribute("Value");
                    if ("clipboard".equalsIgnoreCase(type) )
                    {
                        //X:462;Y:60;W:70;H:30
                        String beforeY = value.split("Y:")[0];
                        String afterY = value.split("Y:")[1];

                        int yvalue = Integer.parseInt( afterY.substring(0, afterY.indexOf(";") ) ) + 20;

                        value =  beforeY + "Y:" + yvalue + afterY.substring(afterY.indexOf(";"));

				        map.put("bounds", getRectangle(value) );
				    }
                    else
                    {
					    //System.out.println("bounds = "+value);
					    map.put("bounds", getRectangle(value));
                    }   
				}

				if( element.getAttribute("Name").equalsIgnoreCase("transitionStepId")){
					//System.out.println("transitionStepId = "+element.getAttribute("Value"));
					map.put("transitionStepId", element.getAttribute("Value"));
				}
				if( element.getAttribute("Name").equalsIgnoreCase("transportLocations") )
				{
					map.put("transportLocations", element.getAttribute("Value"));
				}

				/*if(element.getAttribute("Name").equalsIgnoreCase("source")){
					map.put("source", getPoint(element.getAttribute("Value")));
				}

				if(element.getAttribute("Name").equalsIgnoreCase("target")){
					map.put("target", getPoint(element.getAttribute("Value")));
				}*/
			}
			
			list.add(map);
		}
	}
	
	public Rectangle getRectangle(String bounds){
		
		int length = bounds.length();
		int index = bounds.indexOf(":");
		bounds = bounds.substring(index, length);
		int semiIndex = bounds.indexOf(";");
		//System.out.println("\tX= "+bounds.substring(index, semiIndex));
		int X = Integer.parseInt(bounds.substring(index, semiIndex));
		bounds = bounds.substring(semiIndex+1, bounds.length());
		length = bounds.length();
		index = bounds.indexOf(":");
		bounds = bounds.substring(index, length);
		semiIndex = bounds.indexOf(";");
		//System.out.println("\tY= "+bounds.substring(index, bounds.indexOf(";")));
		int Y = Integer.parseInt(bounds.substring(index, semiIndex));
		bounds = bounds.substring(semiIndex+1, bounds.length());
		length = bounds.length();
		index = bounds.indexOf(":");
		bounds = bounds.substring(index, length);
		semiIndex = bounds.indexOf(";");
		//System.out.println("\tW= "+bounds.substring(index, bounds.indexOf(";")));
		int W = Integer.parseInt(bounds.substring(index, semiIndex));
		bounds = bounds.substring(semiIndex+1, bounds.length());
		length = bounds.length();
		index = bounds.indexOf(":");
		bounds = bounds.substring(index+1, length);
		//System.out.println("\tH="+bounds);
		int H = Integer.parseInt(bounds);
		
		return new Rectangle(X, Y, W, H);
	}
	
	public Point getPoint(String bounds){
		
		int length = bounds.length();
		int index = bounds.indexOf(":");
		bounds = bounds.substring(index, length);
		int semiIndex = bounds.indexOf(";");
		int X = Integer.parseInt(bounds.substring(index, semiIndex));
		bounds = bounds.substring(semiIndex+1, bounds.length());
		length = bounds.length();
		index = bounds.indexOf(":");
		bounds = bounds.substring(index+1, length);
		int Y = Integer.parseInt(bounds);
		
		return new Point(X, Y);
	}
	
	public void insertActivities(Map change, Object[] newCells, String type, int cellNo){
		
		DefaultGraphCell vertex = new DefaultGraphCell();

		vertex.add(new DefaultPort());
		Map map = GraphConstants.createMap();
		
		if( change.containsKey("processStepId") && change.get("processStepId") != null )
		{
			if ( ((String)change.get("processStepId")).length() > 0 )
			{
				GraphConstants.setBackground(map, Color.white);
			}
			else
			{
				GraphConstants.setBackground( map, new Color(213, 221, 253) );
			}
		}
		
		GraphConstants.applyMap(change, map);
		GraphConstants.setBorderColor(map, Color.black);
		//GraphConstants.setBackground(map, Color.white);
		GraphConstants.setOpaque(map, true);
		
		Hashtable attributes = new Hashtable();
		attributes.put(vertex, map);

		graph.getGraphLayoutCache().insert(
				new Object[]{vertex}, 
				attributes,
				null,
				null,
				null);
        if ("clipboard".equalsIgnoreCase( type ) )
        {
            newCells[cellNo] = vertex; 
            graph.setSelectionCells(newCells);
        }
        else
        {
            graph.clearSelection();
        }

	}
	
	public void insertTransitions(Map change, Object[] newEdges, String type, int cellNo){
		
		ConnectionSet cs = new ConnectionSet();
		// Construct Edge with no label
		DefaultEdge edge = new DefaultEdge();

		DefaultPort source = getPort(getActivity((String)change.get("From")));
		DefaultPort target = getPort(getActivity((String)change.get("To")));

		// Create Connection between source and target using edge
		cs.connect(edge, source, target);
		// Create a Map thath holds the attributes for the edge
		Map map = GraphConstants.createMap();
		
		GraphConstants.applyMap(change, map);
		// Add a Line End Attribute
		GraphConstants.setLineEnd(map, GraphConstants.ARROW_TECHNICAL);
		// Construct a Map from cells to Maps (for insert)
		Hashtable attributes = new Hashtable();
		// Associate the Edge with its Attributes
		attributes.put(edge, map);
        
		// Insert the Edge and its Attributes
		graph.getGraphLayoutCache().insert(
			new Object[] { edge },
			attributes,
			cs,
			null,
			null);
		if ("clipboard".equalsIgnoreCase( type ) )
        {
            newEdges[cellNo] = edge;
            graph.setSelectionCells(newEdges);
        }
        else
        {
            graph.clearSelection();
        }
	}
	
	//public void saveToDB(File file){
		//WorkflowDBManager db = new WorkflowDBManager(this);
		//db.saveWorkflowInDB(file);
	//}
	
	public void updateProcessStepId(Document parent, String name, String processStepId){
		
		System.out.println("Updating XPDL = "+"Name = "+name+"ProcessStepId = "+processStepId);
		NodeList nodeList = parent.getElementsByTagName("Activities");
				
		for(int i =0; i<nodeList.getLength();i++){
			Element element = (Element)nodeList.item(i);
			NodeList activity = element.getElementsByTagName("Activity");
			
			for(int j=0; j<activity.getLength(); j++){
				Element tempElement = (Element)activity.item(j);
				
				if(tempElement.getAttribute("name").equalsIgnoreCase(name)){
					NodeList extendedAttribute = tempElement.getElementsByTagName("ExtendedAttribute");
					
					for(int extr = 0; extr<extendedAttribute.getLength(); extr++){
						Element tempExtElement = (Element)extendedAttribute.item(extr);
						
						if(tempExtElement.getAttribute("Name").equalsIgnoreCase("processStepId")){
							System.out.println("Process Step ID updation in XPDL = "+processStepId);
							tempExtElement.setAttribute("Value", processStepId);
						}
					}
				}
			}
		}
		
	}
	
	public String getTooltip(MouseEvent event){
		
		StringBuffer sb = new StringBuffer();
		
		Object cell = graph.getFirstCellForLocation(event.getX(), event.getY());
		if((cell != null) && !graph.getEditor().isGroup(cell) && !( cell instanceof DefaultEdge ) )
        {
			Map attributes = ((DefaultGraphCell)cell).getAttributes();

			String tempStr = null;
			if(attributes.get("value") instanceof String)
            {
				tempStr = (String)attributes.get("value");
                int delimerIndex = -1;
                if ( tempStr != null && tempStr.length() > 0 && tempStr.indexOf("/") > 0 )
                {
                    delimerIndex = tempStr.indexOf("/");
                    if ( delimerIndex != -1 )
                    {
                        tempStr = tempStr.substring(0, delimerIndex);
                    }
                }
			}
			
			sb.append("<html>");
			sb.append("<body bgcolor='#ffffee'><font face='Arial' size='2'>");
			if(tempStr == null){
				tempStr = "";
			}
			sb.append("<STRONG>Process Name : </STRONG>"+tempStr +"<BR>");
			tempStr = (String)attributes.get("Type");
			if(tempStr == null){
				tempStr = "";
			}
			sb.append("<STRONG>Process Type : </STRONG>"+ tempStr+"<BR>");
			tempStr = (String)attributes.get("partnumber");
			if(tempStr == null){
				tempStr = "";
			}
			
			sb.append("<STRONG>Part Number : </STRONG>"+ tempStr+"<BR>");
			/*
			if( attributes.containsKey("mktPartNumber") )
			{
				sb.append("<STRONG>"+Editor.mktgPartTitle+" : </STRONG>"+ (String)attributes.get("mktPartNumber") +"<BR>");
			}

            if( attributes.containsKey("partStatus") )
			{
				sb.append("<STRONG>Part Status : </STRONG>"+ (String)attributes.get("partStatus") +"<BR>");
			}

            if( attributes.containsKey("dupPartNumbers") )
			{
				if( ((String)attributes.get("dupPartNumbers")).length() > 0 )
				sb.append("<STRONG>Equivalent Partnumber : </STRONG>"+ ((String)attributes.get("dupPartNumbers")).substring(0,((String)attributes.get("dupPartNumbers")).length() -1  ) +"<BR>");
			}

			if( attributes.containsKey("goodBinNumber") )
			{
				sb.append("<STRONG>Bin Information : </STRONG> Good Bins  <I>"+ (String)attributes.get("goodBinNumber") +"</I>,  Rejected Bins  <I>"+ (String)attributes.get("rejectedBinNumber") +"</I><BR>");
			}
			
			sb.append("<STRONG>Parameters : </STRONG>");
			String parameters = (String)attributes.get("parameters");
			
			if( parameters != null ){
				Iterator it = parseString(parameters, ';').iterator();
				while(it.hasNext()){
					ArrayList tempList = parseString((String)it.next(), '=');
					if(!tempList.isEmpty() && tempList.size() == 2 ){
						sb.append("<I>"+(String)tempList.get(0)+": "+(String)tempList.get(1)+"</I><BR>");
					}
					else if(!tempList.isEmpty() && tempList.size() == 3 ){
					sb.append("<I>"+(String)tempList.get(0)+": "+(String)tempList.get(2)+"</I><BR>");
					} 
				}
			}
			
			sb.append("<STRONG>Vendors : </STRONG> Locations ");
			
			String vendors = null;
			if( attributes.containsKey("vendors") )
				vendors = (String)attributes.get("vendors");
			
			if( vendors != null )
			{
				ArrayList vendorsList = parseString(vendors,'#');
				if( vendorsList != null && vendorsList.size() > 0 )
				{
					for ( int i=0 ; i < vendorsList.size() ; i++ )
					{
						ArrayList vendorInfo = parseString( (String)vendorsList.get(i) , ';');
						if( vendorInfo != null && vendorInfo.size() > 1 )
						{
							if( ((String)vendorInfo.get(0)).equals("true")  )
							{
								sb.append(" <I>"+ ((String)vendorInfo.get(2)) +"</I><BR>");
							}
						}
					}	  
				}
				
			}
			*/
			sb.append("</font></body></html>");
		}
		return sb.toString();
	}
	
	public ArrayList parseString(String string, char delim){
		
		char[] charArray =string.toCharArray();
		
		ArrayList list = new ArrayList();
		StringBuffer sb = new StringBuffer();
		
		for(int i=0; i<charArray.length; i++)
		{
			if(charArray[i] != delim)
			{
				sb.append(charArray[i]);
			}
			else
			{
				list.add(sb.toString().trim());
				sb.delete(0, sb.length());
			}
		}
		
		list.add(sb.toString().trim()); 
		
		return list;
		
	}
	
	public PortView getSourcePortAt(Point point) {
		// Scale from Screen to Model
		Point tmp = graph.fromScreen(new Point(point));
		//System.out.println("Source Port = "+tmp);
		System.out.println("Source PortView = " + graph.getPortForLocation(tmp.x, tmp.y));
		// Find a Port View in Model Coordinates and Remember
		return graph.getPortViewAt(tmp.x, tmp.y);
	}
	
	protected PortView getTargetPortAt(Point point) {
		// Find Cell at point (No scaling needed here)
		Object cell = graph.getFirstCellForLocation(point.x, point.y);
		// Loop Children to find PortView
		for (int i = 0; i < graph.getModel().getChildCount(cell); i++) {
			// Get Child from Model
			Object tmp = graph.getModel().getChild(cell, i);
			// Get View for Child using the Graph's View as a Cell Mapper
			tmp = graph.getGraphLayoutCache().getMapping(tmp, false);
			// If Child View is a Port View and not equal to First Port
			if (tmp instanceof PortView)
				// Return as PortView
				return (PortView) tmp;
		}
		// No Port View found
		return getSourcePortAt(point);
	}
	
	protected String validateGraph()
	{
		String valid = "";
		ArrayList activities =  getActivities();
		ArrayList edges = getAllTransitions();
			        
		ArrayList fromCells = new ArrayList(); 
		ArrayList toCells = new ArrayList();
				
		valid = checkActivities( activities );
		if( valid.length() < 1 )
		{		
			for( int i=0 ; i < edges.size() ; i++ )
			{
				DefaultEdge edge = (DefaultEdge)edges.get(i);
				if( edge.getSource() != null )
				{
					DefaultGraphCell sourceCell = (DefaultGraphCell)((DefaultPort)edge.getSource()).getParent();
					fromCells.add( sourceCell );
				}
				if( edge.getTarget() != null )
				{
					DefaultGraphCell targetCell = (DefaultGraphCell)((DefaultPort)edge.getTarget()).getParent();
					toCells.add( targetCell );
				}
			}
			// valid = checkTransitions( activities, fromCells, toCells );
		}	
		return valid;
	}
		
	private String checkTransitions ( ArrayList activities, ArrayList fromCells, ArrayList toCells )
	{
		String valid="";
		for( int i=0; i < activities.size() ; i++ )
		{
			DefaultGraphCell cell = ( DefaultGraphCell )activities.get(i);
			if( !fromCells.contains(cell) )
			{
				if( (!cell.getAttributes().containsKey("Type")) ||  (!((String)cell.getAttributes().get("Type")).equals( ProcessTypes.finishedGoods ))  )
					valid = valid +"\n"+ "process "+(String)cell.getAttributes().get("value")+" of type "+(String)cell.getAttributes().get("Type")+ " should have a out going transition "; 
			}
				
			if( !toCells.contains(cell) )
			{
				if( (!cell.getAttributes().containsKey("Type")) ||  (!((String)cell.getAttributes().get("Type")).equals( ProcessTypes.fab ))  )
					valid =  valid +"\n"+ "process "+(String)cell.getAttributes().get("value")+" of type "+(String)cell.getAttributes().get("Type")+  " should have a in coming transition "; 
			}
				
			if( ( cell.getAttributes().containsKey("Type")) &&  (((String)cell.getAttributes().get("Type")).equals( ProcessTypes.finishedGoods )) )
			{
				if( fromCells.contains(cell) )
					valid =  valid +"\n"+ "process "+(String)cell.getAttributes().get("value")+" of type "+(String)cell.getAttributes().get("Type")+  " should not have a out going transition ";
			}
	
			if( ( cell.getAttributes().containsKey("Type")) &&  (((String)cell.getAttributes().get("Type")).equals( ProcessTypes.fab )) )
			{
				if( toCells.contains(cell) )
					valid =  valid +"\n"+ "process "+(String)cell.getAttributes().get("value")+" of type "+(String)cell.getAttributes().get("Type")+  " should not have a in coming transition ";
			}
		}
		System.out.println(valid);
		return valid;	
	}
		
	private String checkActivities(ArrayList activities)
	{
		String valid = "";
			        
		for( int i=0; i < activities.size() ; i++ )
		{
			DefaultGraphCell cell = ( DefaultGraphCell )activities.get(i); 
			Map attributes = cell.getAttributes();
			//System.out.println("Type "+ attributes.get("Type")+"  "+ attributes.get("value") );
			if( attributes.get("Type") == null || ((String)attributes.get("Type")).length() < 1 )
			{
				valid = "All the processes should be configured before Saving";
				break;
			} 
		}
		System.out.println( valid );
		return valid ;
	}

}